﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class DetailServices
    {
        /// <summary>
        /// 获取商品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Product GetProduct(int id)
        {
            using (ShopContext db = new ShopContext())
            {
                Product product = db.Product.SingleOrDefault(p => p.Id == id);
                return product;
            }
        }
        /// <summary>
        /// 获取规格
        /// </summary>
        /// <param name="shopid"></param>
        /// <returns></returns>
        public static List<GuiGe> GetGuiGe(int shopid)
        {
            using (ShopContext db = new ShopContext())
            {

                string sql = string.Format(@"select * from GuiGes where Id in(select GuiGeId from Spu_GuiGe where ShopId={0})", shopid);
                List<GuiGe> guige = db.Database.SqlQuery<GuiGe>(sql).ToList();

                return guige;
            }
        }
        /// <summary>
        /// 获取规格值
        /// </summary>
        /// <param name="shopid"></param>
        /// <returns></returns>
        public static List<GuiGeZhi> GetGuiGeZhis(int shopid)
        {
            using (ShopContext db = new ShopContext())
            {

                string sql = string.Format(@"select * from GuiGeZhis where GuiGeId in (select GuiGeId from Spu_GuiGe where ShopId = {0})", shopid);
                List<GuiGeZhi> guigezhi = db.Database.SqlQuery<GuiGeZhi>(sql).ToList();
                return guigezhi;
            }

        }
        /// <summary>
        /// 获取该用户地址信息
        /// </summary>
        /// <returns></returns>
        public static Address GetAddress(string name)
        {
            using (ShopContext db = new ShopContext()) {
              Address address=  db.Address.FirstOrDefault(p => p.ShouHuoName == name);
                return address;
            }
        }
        /// <summary>
        /// 根据规格值id查出单品
        /// </summary>
        /// <param name="guigezhiid"></param>
        /// <returns></returns>
        public static DanPin GetDanPin(int  guigezhiid)
        {
            using(ShopContext db=new ShopContext())
            {
              var skulist= db.Sku_GuiGeZhi.SingleOrDefault(p => p.GuiGeId == guigezhiid);
                DanPin danpin = db.DanPin.SingleOrDefault(p => p.Id == skulist.SkuId);
                return danpin;
             
            }
        }

    }
}
